package com.weiaibenpao.demo.chislim.hurui.adapte;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.weiaibenpao.demo.chislim.R;
import com.weiaibenpao.demo.chislim.hurui.bean.OfficialMarathonBean;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by wlx on 2017/9/8.
 */

public class OffMarathonAdapter extends RecyclerView.Adapter<OffMarathonAdapter.ViewHolder> {



    private OffMarathonAdapter.OnItemClickListener listener;
    private Context context;
    private LayoutInflater mInflater;
    private List<OfficialMarathonBean.DataBean.MarathonlistBean> list;

    public OffMarathonAdapter(Context context) {
        this.context = context;
        this.mInflater = LayoutInflater.from(context);
        list = new ArrayList<>();
    }

    public void setOnItemClickListener(OffMarathonAdapter.OnItemClickListener listener){
        this.listener=listener;
    }
    public void refresh(List<OfficialMarathonBean.DataBean.MarathonlistBean> list) {
        this.list = list;
        this.notifyDataSetChanged();
    }

    public void loadMore(List<OfficialMarathonBean.DataBean.MarathonlistBean> list) {
        this.list.addAll(list);
        this.notifyDataSetChanged();
    }

   public interface OnItemClickListener{
        void onItemClickListener(OfficialMarathonBean.DataBean.MarathonlistBean bean);
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = mInflater.inflate(R.layout.marathon_item, parent, false);
        ViewHolder viewHolder = new ViewHolder(view);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(final ViewHolder holder, int position) {
        final OfficialMarathonBean.DataBean.MarathonlistBean bean = list.get(position);
        Glide.with(context).load(bean.getMContentImg()).into(holder.mContentImg);
        holder.marathonTitle.setText(bean.getMarathonTitle());
        holder.marathonDate.setText(bean.getRemainTime());
        holder.textNumber.setText("报名人数："+bean.getJoinNumber());
        if (position==list.size()-1){
            holder.viewLine.setVisibility(View.GONE);
        }
        if (bean.getType()==7){
            holder.textTP.setText("个人");
        }else {
            holder.textTP.setText("团队");
        }
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
               listener.onItemClickListener(bean);
            }
        });
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    class ViewHolder extends RecyclerView.ViewHolder {
         View viewLine;
        TextView textNumber,textTP;
        ImageView mContentImg;
        TextView marathonTitle;
        TextView marathonDate;
        CheckBox statusBtn;
        public ViewHolder(View itemView) {
            super(itemView);

            mContentImg = (ImageView) itemView.findViewById(R.id.mContentImg);
            marathonTitle = (TextView) itemView.findViewById(R.id.marathonTitle);
            marathonDate = (TextView) itemView.findViewById(R.id.marathon_date);
            statusBtn = (CheckBox) itemView.findViewById(R.id.status_btn);
            textNumber = ((TextView) itemView.findViewById(R.id.text_number));
            textTP = ((TextView) itemView.findViewById(R.id.text_team_personal));
            viewLine = itemView.findViewById(R.id.view_line);
            textNumber.setVisibility(View.VISIBLE);
            textTP.setVisibility(View.VISIBLE);
            statusBtn.setVisibility(View.GONE);

        }
    }
}
